package com.lewis.module.m2.api.bot.controller;

import com.lewis.api.system.RemoteBotQqService;
import com.lewis.api.system.entity.BotQq;
import com.lewis.common.core.entity.BaseResult;
import com.lewis.common.core.entity.controller.BaseController;
import com.lewis.common.core.entity.page.PageResult;
import com.lewis.module.m2.api.bot.entity.CustomFriend;
import com.lewis.module.m2.api.bot.entity.CustomGroup;
import com.lewis.module.m2.utils.BotUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.contact.ContactList;
import net.mamoe.mirai.contact.Friend;
import net.mamoe.mirai.contact.Group;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * qq机器人账号Controller
 *
 * @author lewis
 * @since 2022-08-07
 */
@Slf4j
@Api(tags = "qq机器人账号")
@RestController
@RequestMapping("/bot/qq")
public class BotQqController extends BaseController {

	@Resource
	private RemoteBotQqService remoteBotQqService;

	@ApiOperation("获取好友列表")
	@GetMapping("/getFriendList")
	public PageResult getFriendList(Long id) {
		BotQq botQq = remoteBotQqService.getById(id);
		if (botQq == null) {
			return PageResult.fail().setMsg("账号不存在");
		}
		for (Bot bot : BotUtils.botList) {
			if (bot.getId() == Long.parseLong(botQq.getQq())) {
				ContactList<Friend> friends = bot.getFriends();
				List<CustomFriend> friendList = new ArrayList<>();
				for (Friend friend : friends) {
					CustomFriend customFriend = new CustomFriend();
					customFriend.setId(friend.getId());
					customFriend.setNick(friend.getNick());
					customFriend.setRemark(friend.getRemark());
					customFriend.setAvatarUrl(friend.getAvatarUrl());
					friendList.add(customFriend);
				}
				return PageResult.ok().setData(friendList).setTotal(friendList.size());
			}
		}
		remoteBotQqService.to0(id);
		return PageResult.fail().setMsg("机器人未登录");
	}

	@ApiOperation("删除指定好友")
	@PostMapping("/deleteFriend/{botQq}/{ids}")
	public BaseResult deleteFriend(@PathVariable Long botQq, @PathVariable Long[] ids) {
		BotUtils.deleteFriend(botQq, ids);
		return BaseResult.ok();
	}

	@ApiOperation("获取群聊列表")
	@GetMapping("/getGroupList")
	public PageResult getGroupList(Long id) {
		BotQq botQq = remoteBotQqService.getById(id);
		if (botQq == null) {
			return PageResult.fail().setMsg("账号不存在");
		}
		for (Bot bot : BotUtils.botList) {
			if (bot.getId() == Long.parseLong(botQq.getQq())) {
				ContactList<Group> groups = bot.getGroups();
				List<CustomGroup> groupList = new ArrayList<>();
				for (Group group : groups) {
					CustomGroup customGroup = new CustomGroup();
					customGroup.setId(group.getId());
					customGroup.setName(group.getName());
					customGroup.setAvatarUrl(group.getAvatarUrl());
					groupList.add(customGroup);
				}
				return PageResult.ok().setData(groupList).setTotal(groupList.size());
			}
		}
		remoteBotQqService.to0(id);
		return PageResult.fail().setMsg("机器人未登录");
	}

	@ApiOperation("登录机器人")
	@GetMapping("/login")
	public BaseResult login(Long id) {
		BotQq botQq = remoteBotQqService.getById(id);
		if (botQq == null) {
			return BaseResult.fail().setMsg("账号不存在");
		}
		String qq = botQq.getQq();
		int flag = 0;
		for (Bot bot : BotUtils.botList) {
			if (bot.getId() == Long.parseLong(qq)) {
				bot.login();
				flag++;
				break;
			}
		}
		if (flag == 0) {
			BotUtils.init(Long.parseLong(botQq.getQq()), botQq.getPassword());
		}
		return remoteBotQqService.to1(id).getCode() == 200 ? BaseResult.ok() : BaseResult.fail();
	}

	@ApiOperation("强退机器人")
	@GetMapping("/logout")
	public BaseResult logout(Long id) {
		BotQq botQq = remoteBotQqService.getById(id);
		if (botQq == null) {
			return BaseResult.fail().setMsg("账号不存在");
		}
		String qq = botQq.getQq();
		for (Bot bot : BotUtils.botList) {
			if (bot.getId() == Long.parseLong(qq)) {
				bot.close();
				BotUtils.botList.remove(bot);
				break;
			}
		}
		return remoteBotQqService.to0(id).getCode() == 200 ? BaseResult.ok() : BaseResult.fail();
	}
}